package com.tedu.teacherweibo.demos.web.controller;

import com.tedu.teacherweibo.demos.web.mapper.CommentMapper;
import com.tedu.teacherweibo.demos.web.pojo.dto.CommentDTO;
import com.tedu.teacherweibo.demos.web.pojo.entity.Comment;
import com.tedu.teacherweibo.demos.web.pojo.entity.User;
import com.tedu.teacherweibo.demos.web.pojo.vo.CommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/comment")
public class CommentController {
    @Autowired
    private CommentMapper commentMapper;

    @PostMapping("/insert")
    public int insert(@RequestBody CommentDTO commentDTO, HttpSession session){
        User user = (User)session.getAttribute("user");
        if(user==null){
            return 2;
        }
        Comment comment = new Comment();
        BeanUtils.copyProperties(commentDTO,comment);
        comment.setCreated(new Date());
        comment.setUserId(user.getId());
        commentMapper.insert(comment);
        return 1;
    }

    @GetMapping("/selectByWeiboId")
    public List<CommentVO> findByWeiboId(Integer id){
        return commentMapper.selectByWeiboId(id);
    }
}
